# 模拟分布式计算 :(

import multiprocessing
import queue
from multiprocessing.managers import BaseManager

class QueueManager(BaseManager):
    pass


task_queue = queue.Queue()
result_queue = queue.Queue()

def get_task_queue():
    global task_queue
    return task_queue

def get_result_queue():
    global result_queue
    return result_queue

def master():

    QueueManager.register('get_task_queue', callable=get_task_queue)
    QueueManager.register('get_result_queue', callable=get_result_queue)

    manager = QueueManager(address=('127.0.0.1', 9527), authkey=b'123')

    manager.start()
    tasks = manager.get_task_queue()
    results = manager.get_result_queue()

    for i in range(10):
        print('put task %s' % i)
        tasks.put('task%s' % i)

    print('任务待处理')

    for i in range(10):
        result = results.get()
        print('result=%s' % result)

    manager.shutdown()
    print('任务处理完毕')


if __name__ == '__main__':
    master()

